<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="userObj.photo"
            @click="imgClickFn"
          />
          <!-- file 选择框 -->
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell
        title="名称"
        is-link
        :value="userObj.name"
        @click="userNameClickFn"
      />
      <van-cell
        title="生日"
        is-link
        :value="userObj.birthday"
        @click="birthdayClickFn"
      />
    </van-cell-group>

    <!-- 编辑姓名对话框 -->
    <van-dialog
      v-model="show"
      title="修改名称"
      show-cancel-button
      :beforeClose="beforeCloseFn"
    >
      <br />
      <input type="text" v-model="userName" v-fofo />
    </van-dialog>

    <!-- 时间框 -->
    <van-popup
      v-model="datetimePickerShow"
      position="bottom"
      round
      :style="{ height: '50%' }"
    >
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="dateCancleFn"
        @confirm="confirmFn"
      />
    </van-popup>
  </div>
</template>

<script>
import {
  editUserProfileAPI,
  updateUserPhotoAPI,
  updateUserProfilesAPI,
} from "@/api";
import Notify from "@/ui/Notify";
import { formatDate } from "@/utils/date";
import { mapMutations } from "vuex";
export default {
  name: "UserEdit",
  data() {
    return {
      userObj: {},
      show: false,
      userName: "",

      minDate: new Date(1920, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(),
      datetimePickerShow: false,
    };
  },
  async created() {
    const res = await editUserProfileAPI();
    this.userObj = res.data.data;
  },
  methods: {
    ...mapMutations(["SET_USER_PHOTO", "SET_USER_NAME"]),
    async onFileChange(e) {
      if (e.target.files.length == 0) return; //用户没选择图片阻止往下
      const theFd = new FormData();
      theFd.append("photo", e.target.files[0]);

      const res = await updateUserPhotoAPI(theFd);
      this.userObj.photo = res.data.data.photo;

      this.SET_USER_PHOTO(res.data.data.photo);
    },
    imgClickFn() {
      this.$refs.iptFile.click();
    },
    userNameClickFn() {
      this.show = true;
      this.userName = this.userObj.name;
    },
    async beforeCloseFn(action, done) {
      if (action == "confirm") {
        // 点击确认
        const reg = /^[a-zA-Z0-9\u4e00-\u9fa5]{1,7}$/;
        if (reg.test(this.userName) === true) {
          //正则通过
          await updateUserProfilesAPI({ name: this.userName });
          this.userObj.name = this.userName;
          this.SET_USER_NAME(this.userObj.name);
          done();
        } else {
          //正则不通过
          Notify({
            type: "warning",
            message: "只能输入1-7位数字字母中文组合！",
          });
          done(false);
        }
      } else {
        // 点击取消
        done();
      }
    },
    birthdayClickFn() {
      this.datetimePickerShow = true;
      this.currentDate = new Date(this.userObj.birthday);
    },
    dateCancleFn() {
      this.datetimePickerShow = false;
    },
    async confirmFn() {
      await updateUserProfilesAPI({
        birthday: formatDate(this.currentDate),
      });
      this.datetimePickerShow = false;
      this.userObj.birthday = formatDate(this.currentDate);
    },
  },
};
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
::v-deep .van-dialog__content {
  text-align: center;
  input {
    text-align: center;
    border: none;
  }
}
</style>
